<template>
  <el-dialog :title="name" width="48%" top="5vh" draggable :close-on-press-escape=false :close-on-click-modal=false
    v-model="show" @before-close="close">
    <div class="main">
      <div class="left">
        <div>
          <div>
            <span style="margin-right:20px">目标语言:</span>
            <el-select style="width:150px;margin-right: 20px;" v-model="recordForm.language" class="m-2"
              placeholder="Select" :popper-class="color === 'night' ? 'nightClass' : ''">
              <el-option v-for="(item, index) in languages" :key="index" :label="item.label" :value="item.value" />
            </el-select>
          </div>
          <div style="margin-top:10px;">
            <span style="margin-right:20px;">输出格式:</span>
            <el-select style="width:150px;margin-right: 20px;" v-model="recordForm.OutFileType" class="m-2"
              placeholder="Select" :popper-class="color === 'night' ? 'nightClass' : ''">
              <el-option v-for="(item, index) in OutFileType" :key="index" :label="item.label" :value="item.value" />
            </el-select>
          </div>
          <div style="width:500px;margin:30px auto 0;">
            <div style="text-align:center;">
              <el-icon style="color:#dcdfe5;font-size:150px;">
                <MessageBox />
              </el-icon>
            </div>
            <li style="color:#909299;font-size:14px;margin-bottom:10px;">
              格式：pdf、doc、docx、ppt、PPtx、jpg、jpeg、png、bmp、txt、xlsx</li>
            <li style="color:#909299;font-size:14px;margin-bottom:10px;">非图单文档≤ 10M，图片类文档≤ 3M，10px≤ 宽高≤4096px</li>
            <!-- <li style="color:#909299;font-size:14px;margin-bottom:10px;">批量文档数量≤10个</li> -->
            <li style="color:#fd6484;font-size:14px;margin-bottom:10px;">文档中的图片暂不支持翻译</li>
          </div>
          <div style="text-align:center;margin-top:10px;">
            <el-upload v-model:file-list="fileList" :show-file-list="false" class="upload-demo" ref="uploads" :on-change="handleChange" :http-request="upgradeClick" :on-exceed="handleExceeds">
              <el-button type="primary" round size="large">
                上传文档
                <el-icon v-if="loading" class="is-loading">
                  <Loading />
                </el-icon>
              </el-button>
            </el-upload>
          </div>
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { connrequest, uploadfile } from '../../api/connrequest'
import TranslationSelect from './TranslationSelect.vue';
export default {
  components: {
    TranslationSelect,
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: () => false
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    name: {
      type: String,
      default: () => ''
    },
  },
  watch: {
    dialogVisible(value) {
      this.show = value
    }
  },
  data() {
    return {
      text: '',
      translatedText: '',
      selectedLanguage: '1',
      fileList: [],
      color:'',
      recordForm: {
        language: '1',
        OutFileType: 'docx'
      },
      languages: [
        { label: '英语 => 中文', value: '1' },
        { label: '中文 => 英文', value: '2' },
        // 添加更多语言
      ],
      OutFileType: [
        { label: 'docx', value: 'docx' },
        { label: 'ppt', value: 'ppt' },
        { label: 'xlsx', value: 'xlsx' },
      ],
      show: false
    }
  },
  mounted(){
    this.color = localStorage.getItem('isNightMode') || 'night'
  },
  methods: {
    translation(result) {
      if (result) {
        this.recordForm[result.binding] = result.translation
      }
    },
    submit() {
      this.$emit('submit', this.recordForm)
    },
    close() {
      this.$emit('closeDialog')
    },
    handleChange() { },
    handleExceeds(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    async upgradeClick(file) {
      const frequencyData = {
        Data:{
          Model: 'DocTS'
        }
      }
      const frequency = await connrequest('CheckResAvailability', frequencyData)
      if(frequency.Status.StatusCode !== 0){
        this.$message.error('文档翻译次数超过当天上限')
        return false
      }
      // 导入授权文件
      if (file.file.size > 1024000000000) {
        this.$refs['uploads'].clearFiles();
        setTimeout(() => {
          loading.close();
          this.$message.warning('文件大小不允许超过1KB');
        }, 1000);
      } else {
        const loading = this.$loading({
          lock: true,
          text: '文档上传中请稍后......',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        let formData = new FormData();
        formData.append('CmdCode', "UploadTranslFile");
        formData.append('FileName', file.file.name);
        formData.append('FileType', file.file.name.split('.')[1]);
        formData.append('LangFrom', this.recordForm.language === '1' ? 'en' : 'zh-CHS');
        formData.append('LangTo', this.recordForm.language === '1' ? 'zh-CHS' : 'en');
        formData.append('UUID', this.generateUUID());
        formData.append('OutFileType', this.recordForm.OutFileType);
        formData.append('EmployeeName', sessionStorage.getItem('Name'));
        formData.append('file', file.file);
        const res = await uploadfile(formData)
        if (res.Status.StatusCode === 0) {
          loading.close();
          this.$message.success('文档上传成功')
          this.$refs['uploads'].clearFiles();
          this.$emit('submit')
        } else {
          this.$message.error(res.Status.Msg2Client)
          this.$refs['uploads'].clearFiles();
          setTimeout(() => {
            loading.close();
          }, 1000)
        }
      }
    },
    generateUUID() {
      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.relatedConditions {
  display: flex;
  align-items: center;
  height: 95px;

  .conditionname {
    width: 120px;
    height: 30px;
    line-height: 30px;
    color: #409EFF;
    border: 2px solid #409EFF;
    text-align: center;
    border-radius: 20px;
    margin-right: 20px;
  }
}

.dialog-footer {
  display: inline-block;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-bottom: 40px;
}

.translation-select {
  position: absolute;
  right: 30px;
  bottom: 30px;
  display: flex;
  align-items: center;
}

.main {
  display: flex;
  justify-content: space-between;
  overflow: hidden;
  background: #fff;
  .left,
  .right {
    position: relative;
    width: 100%;
    height: 430px;
  }

  .copyButton {
    position: absolute;
    bottom: 30px;
    right: 30px;
    z-index: 100;
    color: #409EFF;
  }
}
</style>
<style>
.el-dialog__header {
  padding: 0;
  margin: 0;
}

.el-dialog.is-draggable .el-dialog__header {
  border-bottom: 1px solid #409EFF;
}

.el-dialog__title {
  display: inline-block;
  width: 180px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  background: #409EFF;
  color: #fff;
}

.rounded-checkbox .el-checkbox__inner {
  border-radius: 50%;
}

.rounded-checkbox .el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: #409EFF;
  border-color: #409EFF;
}

.el-checkbox__input.is-checked+.el-checkbox__label {
  color: #409EFF;
}

.el-dialog__footer {
  margin: 0;
  padding: 0;
  text-align: left;
  margin-left: 20px;
  font-size: 14px;
}
</style>
